<template>
  <el-form
    ref="userForm"
    :model="userInfo"
    label-width="120px"
    style="margin-left: 120px; margin-top: 30px"
    :rules="rules"
  >
    <el-form-item label="姓名:" prop="username">
      <el-input v-model="userInfo.username" style="width:300px" />
    </el-form-item>
    <el-form-item label="密码:" prop="password">
      <el-input v-model="userInfo.password" disabled type="password" style="width:300px" />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="doEdit">更新</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getUserId } from '@/api/user'
import { saveUserDetailById } from '@/api/employees.js'
export default {
  name: 'LoginSetting',
  // 导入组件名
  components: {},
  // 父传子,接收变量名
  props: {},
  data() {
    // 这里存放数据
    return {
      // 收集的表单信息
      userInfo: {
        username: '',
        password: ''
      },
      // 表单验证规则
      rules: {
        username: [{ required: true, message: '用户名不能为空', triggler: 'blur' }],
        password: [{ required: true, message: '密码不能为空', triggler: 'blur' }]
      },
      id: this.$route.query.id
    }
  },
  // 计算属性 类似于 data 概念
  computed: {},
  // 监听器 监控 data 中的数据变化
  watch: {},
  created() {
    this.loadUserInfo()
  },
  // 方法集合
  methods: {
    // 确认
    doEdit() {
      // 兜底校验
      this.$refs.userForm.validate(valid => {
        if (!valid) return
        this.updateInfo()
        // 返回之前的页面
        this.$router.back()
      })
    },
    // 获取员工详情
    async loadUserInfo() {
      try {
        const res = await getUserId(this.id)
        console.log('getUserId', res)
        this.userInfo = res.data
      } catch (err) {
        console.log('getUserId', err)
      }
    },
    // 更新员工信息
    async  updateInfo() {
      try {
        const res = await saveUserDetailById(this.userInfo)
        console.log(res)
      } catch (e) {
        console.log(e)
      }
    }

  }
}
</script>

<style scoped>
</style>
